import requests
import sys
import zlib
import json
from lxml import etree

# def getexchange():
    # print(123)
    # url = 'http://web.juhe.cn:8080/finance/exchange/rmbquot'

    # header = {
    #     'User-Agnet': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
    # }

    # params = {
    #     "key": "b4809a1a85ff401b94ea21a8ffff3142"
    # }
    # res = requests.get(url=url, params=params, headers=header)
    
    
#     data_list = []
#     for key,data in result.items():
#         data_dict = {}
#         data_dict["id"] = key
#         data_dict["time"] = data.get("time")
#         data_dict["name"] = data.get("name")
#         data_list.append(data_dict)
#     data_str = json.dumps(data_list)
#     print(type(data_str), data_str)
    
#     return data_str

#     if res.json().get("resultcode") == "200":
#         result = res.json().get("result")[0]
#         zlib_result = zlib.compress(result.encode())
#         return zlib_result
#     else:
#         print(res.json().get("reason"))
#         return res.json().get("reason")
# getexchange()
# import zlib
# a = "[{'data1': {'bankConversionPri': '657.1200', 'date': '2020-11-26', 'fBuyPri': '655.7400', 'fSellPri': '658.5000', 'mBuyPri': '650.4800', 'mSellPri': '658.5000', 'name': '美元', 'time': '10:05:01'}, 'data2': {'bankConversionPri': '783.6900', 'date': '2020-11-26', 'fBuyPri': '780.7900', 'fSellPri': '786.5900', 'mBuyPri': '758.4600', 'mSellPri': '786.5900', 'name': '欧元', 'time': '10:05:01'}, 'data3': {'bankConversionPri': '84.7850', 'date': '2020-11-26', 'fBuyPri': '84.6100', 'fSellPri': '84.9600', 'mBuyPri': '83.9200', 'mSellPri': '84.9600', 'name': '港币', 'time': '10:05:01'}, 'data4': {'bankConversionPri': '6.2976', 'date': '2020-11-26', 'fBuyPri': '6.2743', 'fSellPri': '6.3209', 'mBuyPri': '6.0948', 'mSellPri': '6.3209', 'name': '日元', 'time': '10:05:01'}, 'data5': {'bankConversionPri': '880.1950', 'date': '2020-11-26', 'fBuyPri': '876.9400', 'fSellPri': '883.4500', 'mBuyPri': '851.8600', 'mSellPri': '883.4500', 'name': '英镑', 'time': '10:05:01'}, 'data6': {'bankConversionPri': '484.1200', 'date': '2020-11-26', 'fBuyPri': '482.3300', 'fSellPri': '485.9100', 'mBuyPri': '468.5300', 'mSellPri': '485.9100', 'name': '澳大利亚元', 'time': '10:05:01'}, 'data7': {'bankConversionPri': '505.7600', 'date': '2020-11-26', 'fBuyPri': '503.8900', 'fSellPri': '507.6300', 'mBuyPri': '489.4800', 'mSellPri': '507.6300', 'name': '加拿大元', 'time': '10:05:01'}, 'data8': {'bankConversionPri': '21.6700', 'date': '2020-11-26', 'fBuyPri': '21.5900', 'fSellPri': '21.7500', 'mBuyPri': '20.9200', 'mSellPri': '21.7500', 'name': '泰国铢', 'time': '10:05:01'}, 'data9': {'bankConversionPri': '490.9650', 'date': '2020-11-26', 'fBuyPri': '489.1500', 'fSellPri': '492.7800', 'mBuyPri': '475.1600', 'mSellPri': '492.7800', 'name': '新加坡元', 'time': '10:05:01'}, 'data10': {'bankConversionPri': '724.1650', 'date': '2020-11-26', 'fBuyPri': '721.4900', 'fSellPri': '726.8400', 'mBuyPri': '700.8500', 'mSellPri': '726.8400', 'name': '瑞士法郎', 'time': '10:05:01'}, 'data11': {'bankConversionPri': '105.2900', 'date': '2020-11-26', 'fBuyPri': '104.9000', 'fSellPri': '105.6800', 'mBuyPri': '101.9000', 'mSellPri': '105.6800', 'name': '丹麦克朗', 'time': '10:05:01'}, 'data12': {'bankConversionPri': '82.3100', 'date': '2020-11-26', 'fBuyPri': '82.1400', 'fSellPri': '82.4800', 'mBuyPri': '81.4800', 'mSellPri': '82.4800', 'name': '澳门元', 'time': '10:05:01'}, 'data13': {'bankConversionPri': '160.9550', 'date': '2020-11-26', 'fBuyPri': '160.3600', 'fSellPri': '161.5500', 'mBuyPri': None, 'mSellPri': '161.5500', 'name': '林吉特', 'time': '10:05:01'}, 'data14': {'bankConversionPri': '74.4850', 'date': '2020-11-26', 'fBuyPri': '74.2100', 'fSellPri': '74.7600', 'mBuyPri': '72.0900', 'mSellPri': '74.7600', 'name': '挪威克朗', 'time': '10:05:01'}, 'data15': {'bankConversionPri': '460.3200', 'date': '2020-11-26', 'fBuyPri': '458.6200', 'fSellPri': '462.0200', 'mBuyPri': '445.5000', 'mSellPri': '462.0200', 'name': '新西兰元', 'time': '10:05:01'}, 'data16': {'bankConversionPri': '12.8965', 'date': '2018-08-14', 'fBuyPri': '12.4190', 'fSellPri': '13.3740', 'mBuyPri': '12.4190', 'mSellPri': '13.3740', 'name': '菲律宾比索', 'time': '01:10:04'}, 'data17': {'bankConversionPri': '8.6800', 'date': '2020-11-26', 'fBuyPri': '8.6500', 'fSellPri': '8.7100', 'mBuyPri': '8.3800', 'mSellPri': '8.7100', 'name': '卢布', 'time': '10:05:01'}, 'data18': {'bankConversionPri': '77.3650', 'date': '2020-11-26', 'fBuyPri': '77.0800', 'fSellPri': '77.6500', 'mBuyPri': '74.8700', 'mSellPri': '77.6500', 'name': '瑞典克朗', 'time': '10:05:01'}, 'data19': {'bankConversionPri': '22.3750', 'date': '2018-08-14', 'fBuyPri': '21.5500', 'fSellPri': '23.2000', 'mBuyPri': '21.5500', 'mSellPri': '23.2000', 'name': '新台币', 'time': '01:10:04'}, 'data20': {'bankConversionPri': '176.6900', 'date': '2018-08-14', 'fBuyPri': '167.9000', 'fSellPri': '185.4800', 'mBuyPri': '167.9000', 'mSellPri': '185.4800', 'name': '巴西雷亚尔', 'time': '01:10:04'}, 'data21': {'bankConversionPri': '0.2984', 'date': '2020-11-26', 'fBuyPri': None, 'fSellPri': '0.5968', 'mBuyPri': '0.5755', 'mSellPri': '0.5968', 'name': '韩国元', 'time': '10:05:01'}, 'data22': {'bankConversionPri': '43.4100', 'date': '2020-11-26', 'fBuyPri': '43.2500', 'fSellPri': '43.5700', 'mBuyPri': '41.1400', 'mSellPri': '43.5700', 'name': '南非兰特', 'time': '10:05:01'}}]"
# alib_a = zlib.compress(a.encode())

# # print(alib_a)
# # print(sys.getsizeof(alib_a))

# ss = zlib.decompress(alib_a)
# print(ss.decode(encoding="utf-8"))





def getexchange():
    url = 'http://fx.cmbchina.com/hq/'
    headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
    }

    res = requests.get(url=url,headers=headers)
    tree = etree.HTML(res.text)
    tr_list = tree.xpath('//div[@id="realRateInfo"]/table/tr[position()>1]')

    data={
    'HongKong':format(float(tr_list[0].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'NewZealand': format(float(tr_list[1].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Australia': format(float(tr_list[2].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'USA': format(float(tr_list[3].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Europe' : format(float(tr_list[4].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Canada' : format(float(tr_list[5].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'English' : format(float(tr_list[6].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Japaense' : format(float(tr_list[7].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Singapore' : format(float(tr_list[8].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'Switzerland' : format(float(tr_list[9].xpath("./td[4]/text()")[0].replace('\r',"").replace("n","").strip())/100,".4f"),
    'time':tr_list[2].xpath("./td[8]/text()")[0].replace('\r',"").replace("n","").strip()
    }
 
    return data
